package com.gongguizhijia.Util;

import com.gongguizhijia.Constants.Constants;
import com.gongguizhijia.pojo.DataStreamModel;
import org.apache.commons.lang3.Conversion;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Chenglei
 * @date 2021/6/11 15:22
 * 方舟协议 公共方法
 */
public class ArkProtocolRules {

    public static final String[] ARR = Utils.getArrForHex();
    public static final String BLANK = " ";

    /**
     * 第一种情况： 都能整除8
     * 参数 单位  bit
     * 获取   StartPosition   EndPosition   取位结果
     *
     * @param startPlace
     * @param dataLength
     * @param order
     * @return
     */
    public static Map<String, String> commonMethod1(Integer startPlace, Integer dataLength, String order) {

        Map<String, String> map = new HashMap<String, String>(16);
        if (startPlace == null || dataLength == null) {
            map.put("error", "数据源相关数据为空");
        }
        String hexStartPosition = null;
        String hexEndPosition = null;

        Integer orderLength = 0;
        if (order.replace(BLANK, "").length() % 2 != 0) {
            orderLength = order.replace(" ", "").length() / 2 + 1;
        } else {
            orderLength = order.replace(" ", "").length() / 2;
        }
        Integer startPosition = startPlace / 8 + orderLength + 06;
        //转换成16进制
        hexStartPosition = Utils.intToHex(startPosition);
        if (hexStartPosition.length() <= 1) {
            hexStartPosition = "0" + hexStartPosition;
        }
        Integer endPosition = startPosition + dataLength / 8;
        //转换成16进制
        hexEndPosition = Utils.intToHex(endPosition);
        if (hexEndPosition.length() <= 1) {
            hexEndPosition = "0" + hexEndPosition;
        }

        String startPositionSub = "X" + hexStartPosition;
        String getResult = "";
        int iForResult = 0;
        for (int i = 0; i < ARR.length; i++) {
            if (startPositionSub.equals(ARR[i])) {
                iForResult = i;
            }
        }
        for (int i = iForResult; i < (iForResult + dataLength / 8); i++) {
            getResult += ARR[i];
        }

        map.put(Constants.START_POSITION, hexStartPosition);
        map.put(Constants.END_POSITION, hexEndPosition);
        map.put(Constants.GET_RESULT, getResult);
        return map;
    }

    /**
     * 第二种情况 ： 不能被8整除的
     * 参数 单位 bit
     * 获取   StartPosition   EndPosition
     *
     * @param order
     * @return
     */
    public static Map<String, String> commonMethod2(String order) {

        Map<String, String> map = new HashMap<String, String>(16);

        String hexStartPosition = null;
        String hexEndPosition = null;

        Integer orderLength = 0;
        if (order.replace(BLANK, "").length() % 2 != 0) {
            orderLength = order.replace(BLANK, "").length() / 2 + 1;
        } else {
            orderLength = order.replace(BLANK, "").length() / 2;
        }
        Integer startPosition = 00 + orderLength + 06;
        //转换成16进制
        hexStartPosition = Utils.intToHex(startPosition);
        if (hexStartPosition.length() <= 1) {
            hexStartPosition = "0" + hexStartPosition;
        }
        hexEndPosition = "FF";

        map.put(Constants.START_POSITION, hexStartPosition);
        map.put(Constants.END_POSITION, hexEndPosition);
        return map;
    }

    /**
     * 其他规则 ======》无对应的 方舟协议
     * 单位 bit
     * （ 如果表格数据已经是不用除8的数，则需要乘以8，放入方法参数位置）
     *
     * @return
     */
    public static String getForOtherProtocol(Integer startPlace, Integer dataLength, String order) {

        if (startPlace == null || dataLength == null) {
            return "数据源相关数据为空";
        }

        String hexStartPosition = null;
        Integer subitemDataLengthResult = 0;
        if (startPlace != null) {
            if (startPlace % 8 != 0) {
                startPlace = startPlace / 8 + 1;
            } else {
                startPlace = startPlace / 8;
            }
            Integer orderLength = 0;
            if (order.replace(BLANK, "").length() % 2 != 0) {
                orderLength = order.replace(BLANK, "").length() / 2 + 1;
            } else {
                orderLength = order.replace(BLANK, "").length() / 2;
            }
            Integer startPosition = startPlace + orderLength + 06;
            //转换成16进制
            hexStartPosition = Utils.intToHex(startPosition);
            if (hexStartPosition.length() <= 1) {
                hexStartPosition = "0" + hexStartPosition;
            }
            //取几位
            if (dataLength % 8 != 0) {
                subitemDataLengthResult = dataLength / 8 + 1;
            } else {
                subitemDataLengthResult = dataLength / 8;
            }
        }
        String startPositionSub = "X" + hexStartPosition;
        String result = "";
        int iForResult = 0;
        for (int i = 0; i < ARR.length; i++) {
            if (startPositionSub.equals(ARR[i])) {
                iForResult = i;
            }
        }
        for (int i = iForResult; i < (iForResult + subitemDataLengthResult); i++) {
            result += ARR[i];
        }
        return result;
    }

    /**
     * 对于 RD022协议  获取解析公式(有/无符号浮点型)
     * 参数单位 bit
     *
     * @param startPlace
     * @param dataLength
     * @param order
     * @return
     */
    public static String getFormulaByRD022(Integer startPlace, Integer dataLength, String order) {
        //判断参数
        if (startPlace == null || dataLength == null || order == null) {
            return "数据源相关数据为空 ，无法获取";
        }
        //调用获取结果 针对下面调用的方法 仅仅可以获取取位结果
        String formula = getForOtherProtocol(startPlace, dataLength, order);
        //拼接处理解析公式
        formula = "{\"Switch_Type\":\"[" + formula + "]\"}";
        //返回数据
        return formula;
    }

    /**
     * 对于 RK001协议  获取解析公式(16进制显示)  (BCD)
     * 参数单位 bit
     *
     * @param startPlace
     * @param dataLength
     * @param order
     * @return
     */
    public static String getFormulaByRK001(Integer startPlace, Integer dataLength, String order) {
        //判断参数
        if (startPlace == null || dataLength == null || order == null) {
            return "数据源相关数据为空 ，无法获取";
        }
        //调用获取结果 针对下面调用的方法 仅仅可以获取取位结果
        Map<String, String> map = commonMethod1(startPlace, dataLength, order);
        //拼接处理解析公式
        String formula = "{\"start_position\":\"" + map.get(Constants.START_POSITION) + "\",\"end_position\":\"" + map.get(Constants.END_POSITION) + "\"}";
        //返回数据
        return formula;
    }

    /**
     * 对于 RK002协议  获取解析公式(ASCII)
     * 参数单位 bit
     *
     * @param startPlace
     * @param dataLength
     * @param order
     * @return
     */
    public static String getFormulaByRK002(Integer startPlace, Integer dataLength, String order) {
        //判断参数
        if (startPlace == null || dataLength == null || order == null) {
            return "数据源相关数据为空 ，无法获取";
        }
        //调用获取结果 针对下面调用的方法 仅仅可以获取取位结果
        Map<String, String> map = commonMethod1(startPlace, dataLength, order);
        //拼接处理解析公式
        String formula = "{\"start_position\":\"" + map.get(Constants.START_POSITION) + "\",\"end_position\":\"" + map.get(Constants.END_POSITION) + "\"}";
        //返回数据
        return formula;
    }

    /**
     * 对于 RD001协议  获取解析公式(数值)
     * 参数单位 bit
     *
     * @param startPlace
     * @param dataLength
     * @param order
     * @return
     */
    public static String getFormulaByRD001(Integer startPlace, Integer dataLength, String order) {
        //判断参数
        if (startPlace == null || dataLength == null || order == null) {
            return "数据源相关数据为空 ，无法获取";
        }
        //调用获取结果 针对下面调用的方法 仅仅可以获取取位结果
        String formula = getForOtherProtocol(startPlace, dataLength, order);
        //拼接处理解析公式
        formula = "{\"Switch_Type\":\"[" + formula + "]\"}";
        //返回数据
        return formula;
    }

    /**
     * 对于 RD009协议
     * 参数 单位 bit
     *
     * @param startPlace
     * @param dataLength
     * @param order
     * @param coefficient
     * @param offset
     * @return
     */
    public static String getFormulaByRD009(Integer startPlace, Integer dataLength, String order, String coefficient, String offset) {
        //判断参数
        if (startPlace == null || dataLength == null || order == null) {
            return "数据源相关数据为空 ，无法获取";
        }
        String startPosition = null;
        String endPosition = null;
        Map<String, String> map;
        if (startPlace % 8 == 0 && dataLength % 8 == 0) {
            map = commonMethod1(startPlace, dataLength, order);
        } else {
            map = commonMethod2(order);
        }
        startPosition = map.get(Constants.START_POSITION);
        endPosition = map.get(Constants.END_POSITION);
        coefficient = Utils.subNumberText(coefficient);
        offset = Utils.subNumberText(offset);

        String formula = "{\"B\":\"" + coefficient + "\",\"C\":\"" + offset + "\",\"start_position\":\"" + startPosition + "\",\"end_position\":\"" + endPosition + "\"}";
        return formula;
    }

    /**
     * 对于 协议 RD001
     * 单位 bit
     *
     * @param startPlace
     * @param dataLength
     * @param order
     * @param coefficient
     * @param offset
     * @return
     */
    public static String getFormulaByRD001(Integer startPlace, Integer dataLength, String order, String coefficient, String offset) {
        //判断参数
        if (startPlace == null || dataLength == null || order == null) {
            return "数据源相关数据为空 ，无法获取";
        }
        coefficient = Utils.subNumberText(coefficient);
        offset = Utils.subNumberText(offset);
        String result = getForOtherProtocol(startPlace, dataLength, order);
        if (coefficient.startsWith("-")) {
            coefficient = "(" + coefficient + ")";
        }
        if (!offset.startsWith("-")) {
            offset = "+" + offset;
        }
        if ("+0".equals(offset)) {
            offset = "";
        }
        String formula = "{\"Switch_Type\":\"[" + result + "]*" + coefficient + offset + "\"}";
        return formula;
    }

    /**
     * 对于  协议 RD008
     *
     * @param startPlace
     * @param dataLength
     * @param order
     * @param coefficient
     * @param offset
     * @return
     */
    public static String getFormulaByRD008(Integer startPlace, Integer dataLength, String order, String coefficient, String offset) {
        //判断参数
        if (startPlace == null || dataLength == null || order == null) {
            return "数据源相关数据为空 ，无法获取";
        }
        coefficient = Utils.subNumberText(coefficient);
        offset = Utils.subNumberText(offset);
        String result = getForOtherProtocol(startPlace, dataLength, order);
        if (coefficient.startsWith("-")) {
            coefficient = "(" + coefficient + ")";
        }
        if (!offset.startsWith("-")) {
            offset = "+" + offset;
        }
        if ("+0".equals(offset)) {
            offset = "";
        }
        String formula = "{\"Switch_Type\":\"[" + result + "]&FFFF>=0000<=7FFF:[" + result + "]*" + coefficient + ";&FFFF>=8000<=FFFF:[" + result + "]*" + coefficient + offset + "\"}";
        return formula;
    }
}
